<?php 
  require_once("includes/config.php");

  $length = 20;
  
  if(isset($_GET['c']))
  {
    echo 0;
    die();
  }
  if( isset($_GET['page'])  && !empty($_GET['page'] ))
  {
    $page =  filter_var($_GET['page'], FILTER_SANITIZE_NUMBER_INT);
    $start = $page*20 - 20;
  }
  else
  {
    $start = 0;
    $page = 1;
  }

  $sql = 'SELECT a. * , c. *
          FROM apartments a
          LEFT JOIN (

          SELECT apartment_id, MAX( updated_at ) MaxDate
          FROM contracts
          GROUP BY apartment_id
          )MaxDates ON a.id = MaxDates.apartment_id
          LEFT JOIN contracts c ON MaxDates.apartment_id = c.apartment_id
          AND MaxDates.MaxDate = c.updated_at where a.owner_id=?';
          
  $sqlCount = 'SELECT COUNT(*) FROM apartments a
          LEFT JOIN (

          SELECT apartment_id, MAX( updated_at ) MaxDate
          FROM contracts
          GROUP BY apartment_id
          )MaxDates ON a.id = MaxDates.apartment_id
          LEFT JOIN contracts c ON MaxDates.apartment_id = c.apartment_id
          AND MaxDates.MaxDate = c.updated_at where a.owner_id=?';
  $binds = array();
  
  $binds['owner_id'] = $_SESSION['user_id'];

  if(isset($_GET['apart_id']) && $_GET['apart_id'] != -1) 
  {
    if(!empty($binds))
      $and = " and ";
    else
      $and = " ";
           
    $binds['id'] = $_GET['apart_id'];
    $sql .= $and."a.id  = ?";
    $sqlCount .= $and."a.id  = ?";
  } 
        
  if(isset($_GET['apart_status']) && $_GET['apart_status'] != -1 )
  {
    if(!empty($binds))
      $and = " and ";
    else
      $and = "  ";
          
    $binds['status'] = $_GET['apart_status'];
    $sql .= $and."a.status  = ?";
    $sqlCount .= $and."a.status  = ?";
  }
        
  /*if(isset($_GET['rent_type']) && $_GET['rent_type'] != -1 )
  {
    if(!empty($binds))
      $and = " and ";
    else
      $and = "  ";
          
    $binds['rent_type'] = $_GET['rent_type'];
    $sql .= $and."apartments.rent_type = ?";
    $sqlCount .= $and."apartments.rent_type = ?";
  }*/
        
  if(!empty($_GET['rooms#']))
  {
    if(!empty($binds))
      $and = " and ";
    else
      $and = "  ";
          
    $binds['rooms#'] = $_GET['rooms#'];
    $sql .= $and."rooms  = ?";
    $sqlCount .= $and."rooms = ?";
  }
        
  if(!empty($_GET['floor#']))
  {
    if(!empty($binds))
      $and = " and ";
    else
      $and = "  ";
          
    $binds['floor#'] = $_GET['floor#'];
    $sql .= $and."floor = ?";
    $sqlCount .= $and."floor = ?";
  }
        
  if(isset($_GET['apart_type']) && $_GET['apart_type'] != -1)
  {
    if(!empty($binds))
      $and = " and ";
    else
      $and = "  ";
          
    $binds['apart_type'] = $_GET['apart_type'];
    $sql .= $and."room_type = ?";
    $sqlCount .= $and."room_type = ?";
  }
  $sql .= " LIMIT ?,?";
  $binds['start'] = $start;
  $binds['end'] = $length;
  
  //dump($sql);
  $aprts = query($sql,$binds);
  $Count = query($sqlCount,$binds);
  $totalPages = $Count[0]['COUNT(*)'];
  
  
  render("apartments.php", array("title"=>"إدارة الوحدات","aprts"=>$aprts,"totalPages"=>$totalPages,"page"=>$page,"binds"=>$binds,"cooler_type"=>$cooler_type));

?>